using System;
using System.Collections.Generic;
using System.Text;

namespace Eve.Data
{
    public class AssemblyLineStation : EveAssociation<Station, AssemblyLineType>
    {
        private int quantity;
        private int stationTypeId;
        private int ownerId;
        private int solarSystemId;
        private int regionId;

        private StationType stationType;
        private EveObject owner;
        private SolarSystem solarSystem;
        private Region region;

        private List<AssemblyLineStationCostLog> costLogs;

        public Station Station
        {
            get { return From; }
        }
        public AssemblyLineType AssemblyLineType
        {
            get { return To; }
        }
        public int Quantity
        {
            get { return quantity; }
        }
        public StationType StationType
        {
            get
            {
                if (stationType == null)
                    stationType = new StationType(db, stationTypeId);
                return stationType;
            }
        }
        public EveObject Owner
        {
            get
            {
                if (owner == null)
                    owner = EveData.GetObject(db, ownerId);
                return owner;
            }
        }
        public SolarSystem SolarSystem
        {
            get
            {
                if (solarSystem == null)
                    solarSystem = new SolarSystem(db, solarSystemId);
                return solarSystem;
            }
        }
        public Region Region
        {
            get
            {
                if (region == null)
                    region = new Region(db, regionId);
                return region;
            }
        }
        public List<AssemblyLineStationCostLog> CostLogs
        {
            get
            {
                if (costLogs == null)
                {
                    costLogs = new List<AssemblyLineStationCostLog>();
                    string query = "SELECT * FROM ramAssemblyLineStationCostLogs WHERE stationID=" + Station.Id + " AND assemblyLineTypeID=" + AssemblyLineType.Id;
                    foreach (DbDataRow row in db.ExecuteQuery(query))
                        costLogs.Add(new AssemblyLineStationCostLog(row));
                }
                return costLogs;
            }
        }

        internal AssemblyLineStation(DbDataRow dr)
            : base(dr.Database, new Station(dr.Database, dr["stationID"]), new AssemblyLineType(dr.Database, dr["assemblyLineTypeID"]))
        {
            Fill(dr);
        }
        internal AssemblyLineStation(DbDataRow dr, Station station)
            : base(dr.Database, station, new AssemblyLineType(dr.Database, dr["assemblyLineTypeID"]))
        {
            Fill(dr);
        }
        internal AssemblyLineStation(DbDataRow dr, AssemblyLineType type)
            : base(dr.Database, new Station(dr.Database, dr["stationID"]), type)
        {
            Fill(dr);
        }


        private void Fill(DbDataRow dr)
        {
            quantity = dr["quantity"];
            stationTypeId = dr["stationTypeID"];
            ownerId = dr["ownerID"];
            solarSystemId = dr["solarSystemID"];
            regionId = dr["regionID"];
        }
    }
}
